package com.CMS.client.ui.view;

import com.CMS.client.ItemMenuService;
import com.CMS.client.ItemMenuServiceAsync;
import com.CMS.client.datasource.ItemMenuDataSource;
import com.CMS.client.datasource.MenuDataSource;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.TitleOrientation;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class CreateItemMenuView extends VLayout{
	private static final String DESCRIPTION = "NewItemMenuView";
	private TextItem name;
	private SelectItem menu;
	private SelectItem item;
	private Button aceptar;
    private Button cancelar;
    
    public CreateItemMenuView(){
    	super();
		
		name = new TextItem();
		name.setTitle("Nombre del Men&uacute;");
		menu = new SelectItem();
		menu.setTitle("Men&uacute;");		
		menu.setAccessKey("id");
		menu.setValueField("id");
		menu.setDisplayField("description");
		menu.setOptionDataSource(new MenuDataSource());
		item = new SelectItem();
		item.setTitle("Item de Men&uacute;");		
		item.setAccessKey("id");
		item.setValueField("id");
		item.setDisplayField("description");
		item.setOptionDataSource(new ItemMenuDataSource());
		
		final DynamicForm form = new DynamicForm();
		form.setWidth(400);
		form.setIsGroup(true);
        form.setTitleOrientation(TitleOrientation.LEFT);   
        form.setAlign(Alignment.CENTER);
        form.setPadding(20);
        form.setFields(name, menu, item);
        
        VLayout layout = new VLayout(10);
		layout.setWidth100();
		layout.setHeight100();
		layout.setLayoutMargin(10);     	
		layout.setDefaultLayoutAlign(Alignment.CENTER);	
	    layout.addMember(form);		
		
		HLayout h = new HLayout();
		h.setWidth(200);
		h.setMembersMargin(5);
		
		aceptar = new Button("Guardar");
		aceptar.setIcon("icons/16/buttonNew.png");
		aceptar.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				ItemMenuServiceAsync rpc = GWT.create(ItemMenuService.class);
				rpc.create(name.getValueAsString(), new Long(menu.getValue().toString()),
						   (item.getValue() == null) ? 0 : new Long(item.getValue().toString()), new AsyncCallback<Void>() {
							@Override
							public void onFailure(Throwable caught) {
								SC.say(caught.getMessage());
							}

							@Override
							public void onSuccess(Void result) {
								name.clearValue();
								SC.say("Item creado correctamente.");
							}
						});
			}
		});
		
		cancelar = new Button("Cancelar");
		cancelar.setIcon("icons/16/buttonCancel.png");
		cancelar.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				name.clearValue();
			}
		});
		
		h.addMember(aceptar);
		h.addMember(cancelar);
		layout.addMember(h);
		layout.setMargin(100);
		
		VLayout content = new VLayout();
		content.setWidth100();
		content.setHeight100();
		content.setLayoutMargin(10);  
        content.setMembersMargin(40);   
            	
		content.setAlign(Alignment.CENTER);	
		content.addMember(layout);		
		content.setBorder("2px solid #C0C3C7");	       
		
		this.addMember(content);
        this.draw();
    }
    
    
    public static class Factory implements ContextAreaFactory{
		private String id;
		 
		public Canvas create(){
			//hay que crear la vista que vamos a llamar
			CreateItemMenuView view = new CreateItemMenuView();
			id = view.getID();
			return view;
	    }
		 
		public String getID(){
			return id;
		}

		public String getDescription(){
			return DESCRIPTION;
		}
	 }
}
